<?php
namespace app\merchant\controller;

use app\common\model\SelfTakeModel;

class SelfTake extends Common
{
    public function index(){
        $where =[
            'self_take_merchant_id' => session('merchant.merchant_id'),
        ];
        $page_number = 7;
        $data['self_take_list'] = SelfTakeModel::self_take_sel($where, $page_number);
        $data['page'] = $data['self_take_list']->render();
        return view('index', ['data' => $data]);
    }

    //添加自提点页面
    public function add(){
        return view('add');
    }

    //添加自提点数据
    public function do_add(){
        if(!session('?merchant')){
            return retJson('', 300, '你还未登录，请先去登录');
        }
        $validate = validate('selftake');
        $validate->scene('upload');
        if(!$validate->check(input('post.'))){
            $error_msg = $validate->getError();
            return returnJson('', 300, $error_msg);
        }
        $data = input('post.');
        $data['self_take_merchant_id'] = session('merchant.merchant_id');
        $ret = SelfTakeModel::self_take_add($data);
        if(!$ret){
            return returnJson('', 300, '添加自提点失败');
        }
        return returnJson('', 300, '添加自提点成功');
    }

    //修改自提点页面
    public function edit(){
        $self_take_id = input('self_take_id');
        $data['self_take'] = SelfTakeModel::get($self_take_id);
        if(!$data['self_take']){
            return returnJson('', 300, '该自提点不存在');
            //$this->error('该自提点不存在')
        }
        return view('edit', ['data' => $data]);
    }

    //修改自提点数据
    public function do_edit(){
        $validate = validate('selftake');
        $validate->scene('upload');
        if(!$validate->check(input('post.'))){
            $error_msg = $validate->getError();
            return returnJson('', 300, $error_msg);
        }
        $self_take_id = input('self_take_id');
        $self_take = SelfTakeModel::get($self_take_id);
        if(!$self_take){
            return returnJson('', 300, '该自提点不存在');
        }
        if($self_take['self_take_merchant_id'] != session('merchant.merchant_id')){
            return returnJson('', 300, '该自提点不属于你');
        }
        $where =[
            'self_take_id' => $self_take_id,
        ];
        $data = input('post.');
        $ret = SelfTakeModel::self_take_edit($data, $where);
        if(!$ret){
            return returnJson('', 300, '修改自提点失败');
        }
        return returnJson('', 200, '修改自提点成功');
    }

    //删除商户自提点
    public function del(){
        $self_take_id = input('self_take_id');
        $self_take = SelfTakeModel::get($self_take_id);
        if(!$self_take){
            return returnJson('', 300, '该自提点不存在，请刷新页面后，重新删除');
        }
        if($self_take['self_take_merchant_id'] != session('merchant.merchant_id')){
            return returnJson('', 300, '该自提点不属于你');
        }
        $where = [
            'self_take_id' => input('self_take_id'),
        ];
        $ret = SelfTakeModel::self_take_del($where);
    }
}
